﻿using System;
using System.Collections.Generic;

namespace XNET.Extension
{
    /// <summary>
    /// 整型扩展
    /// </summary>
    public static class IntExtension
    {
        /// <summary>
        /// 转换为序号
        /// </summary>
        /// <param name="value">值</param>
        /// <param name="length">长度</param>
        /// <returns></returns>
        public static string ToSeqNo(this int value, int length)
        {
            var newCode = value.ToString();
            var paddingChar = '0';
            if (newCode.Length < length)
            {
                newCode = newCode.PadLeft(value, paddingChar);
            }
            return newCode;
        }

        /// <summary>
        /// Runs a function based on the number of times specified and returns the results
        /// </summary>
        /// <typeparam name="T">Type that gets returned</typeparam>
        /// <param name="Count">Number of times the function should run</param>
        /// <param name="Function">The function that should run</param>
        /// <returns>The results from the function</returns>
        public static IEnumerable<T> Times<T>(this int Count, Func<int, T> Function)
        {
            var ReturnValue = new System.Collections.Generic.List<T>();
            for (var x = 0; x < Count; ++x)
            {
                ReturnValue.Add(Function(x));
            }
            return ReturnValue;
        }

        /// <summary>
        /// Runs an action based on the number of times specified
        /// </summary>
        /// <param name="Count">Number of times to run the action</param>
        /// <param name="Action">Action to run</param>
        public static void Times(this int Count, Action<int> Action)
        {
            for (var x = 0; x < Count; ++x)
            {
                Action(x);
            }
        }
    }
}
